/* jshint esversion: 6 */
import axios from 'axios';
import cookie from 'react-cookies';
import {createHashHistory} from "history";

 /**
  * Do something  before sending request
  */
 axios.interceptors.request.use(
     config => {

         config.headers.Authorization = 'Basic enV1bF9zZXJ2ZXI6c2VjcmV0';

         let access_token=cookie.load('access_token');

         if (access_token===undefined){
             return config;
         }

             config.params = {
                 "access_token":access_token,
                 ...config.params
             }

         return config;
     },
     error => {
         return Promise.error(error);
     }
 )
 /**
  * Do something before handling response
  */
 axios.interceptors.response.use(response=>{
     return response;
 },error => {
     if(error.response.status === 401){
         let history = createHashHistory();
         history.push({
             pathname: "/"
         });
         history.go();
     }
     return Promise.resolve(error.response);
 });

 export default axios;
